package Waya::App::ImportGoTools;

use warnings;
use strict;

# - In order to be able to incorporate the GoTools Tsumego of the Day collection
#   (harvested daily from http://alpha.qmul.ac.uk/~ugah006/tsumego/), it's
#   necessary to rewrite the SGF files:
#   - first, files are edited in-place, so be sure to have a backup
#   - fix passes (indicated by GoTools by W[tt] or B[tt], should be W[], B[])
#   - remove the GN, PL, HA, KM and SY properties
#   - set FF[4] (is currently 3)
#   - the second node (containing only labels) should be deleted
#   - the first node is always the correct solution
#   - all other variations are wrong and should have triangle marks on their
#     first move
# 
#   - files named prob-1-*.sgf are easier problems; files named prob-2-*.sgf are
#     harder ones; they should be put in subdirectories named "easier" and
#     "harder".

our $VERSION = '0.1';

use base 'Waya::App::FileIterator';

sub process_game {
    my $self = shift;

    # replace [tt] with [] (passing)

    my $info = $self->game->game_info_node;

    # munge game info; remove a few unnecessary properties that GoTools
    # generates

    delete $info->{$_} for qw/GN PL HA KM SY/;
    $info->{FF} = 4;

    # skip the second node, as it only contains labels and a comment
    # ('"A" is the proper move')

    $info->{next} = $info->{next}{next};

    # $info->{next} is now the third node, which should be a list of
    # variations.  The first variation is the correct one, the others are
    # wrong. Mark the first move of the wrong variations with a triangle.

    my $variation_no = 0;
    for my $variation ($info->{next}->variations) {
        next unless $variation_no++;   # skip the first (correct) variation
        $variation->[0]->{TR} = $variation->[0]->move;
    }
}


1;
